<?php
namespace app\system\model;

use think\Model;

class Log extends Model
{
    protected $append = [
        'status_text',
    ];
    /**
     * 根据条件获取配置列表信息
     */
    public static function getAll($data, $map = [])
    {
        try {
            $order = ['create_time' => 'desc'];
            if (!empty($data['start_time']) && empty($data['end_time'])) {
                $start_time = strtotime($data['start_time']);
                $map[] = ['create_time', 'between', [$start_time, time()]];
            }
            if (!empty($data['start_time']) && !empty($data['end_time'])) {
                $start_time = strtotime($data['start_time']);
                $end_time = strtotime($data['end_time']);
                $map[] = ['create_time', 'between', [$start_time, $end_time]];
            }
            if (!empty($data['key'])) {
                $map[] = ['auth_user_name|ip|ipaddr', 'like', "%" . $data['key'] . "%"];
            }
            if (empty($data['limit'])) {
                $data['limit'] = 10;
            }
            $res = self::where($map)
                ->order($order)
                ->paginate($data['limit']);
        } catch (\Exception $e) {
            return tofalse($e->getMessage() . '问题行：' . $e->getLine());
        }
        return $res;
    }
    /**
     * 添加/编辑信息
     */
    public static function info($data)
    {
        try {

            if (!empty($data['id'])) {
                $res = self::find($data['id']);
                $res->save($data);
            } else {
                self::create($data);
            }
        } catch (\Exception $e) {
            return tofalse($e->getMessage() . '问题行：' . $e->getLine());
        }
        return true;
    }
    /**
     * 根据字段获取配置信息
     */
    public static function getOne($val, $map = [])
    {
        if (empty($map)) {
            $map = ['id' => $val];
        }
        $res = self::where($map)->find();
        return $res;
    }
    /**
     * 批量操作
     */
    public static function handleAll($param, $type = 0)
    {
        try {
            //判断是否是批量删除
            if ($type == 2) {
                self::destroy(explode(',', $param));
            } else {
                self::where('id', 'in', $param)->update(['status' => $type]);
            }
            return true;
        } catch (\Exception $e) {
            return tofalse($e->getMessage() . '问题行：' . $e->getLine());
        }
    }
    public function getStatusTextAttr($val, $data)
    {
        return $data['status'] != 200 ? '失败100' : '成功200';
    }
}
